//
//  meRemindSettingVC_Change.swift
//  DingDongZP
//
//  Created by 三秒文化 on 16/7/2.
//  Copyright © 2016年 51ledian.com. All rights reserved.
//

import UIKit

class meRemindSettingVC_Change: LDBaseCustomizeVC,UITableViewDelegate,UITableViewDataSource {
    
    enum remindMeType : Int {
        //暂时简单处理
//        case Now = 0
//        case Day
//        case WeekDay
        case On = 1
        case Off = 0
    }
    var selectView:popActionSheetView?
    var model:LDPushNoticeModel?
    var tableView:LDBaseTableView!
    
    let dictTitleList = [
//        ["headtitle":"声音与震动","title":["声音与震动"]],
        ["headtitle":"通知提醒","title":["有人对我的资料感兴趣","有人查看了我的资料","有人新发布了我期望的职位"]],
        ]
    
    let defaultTitleList = ["不提醒","不提醒","不提醒"]
    let noticeTitleList = ["不提醒","提醒"];
    
    let selValueList = ["off","on"]
    
    let jobfinderSelType = ["resume_interested","resume_viewed","newjob"]
    let bossSelType = ["job_interested","job_viewed","newjobfinder"]
    
    let descTitleArr = [String]()
    //网络返回的 提醒数据
    var noticeValue = [String]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupTableView()
        requestNet()
        
    }
    
    override func viewWillDisappear(animated: Bool) {
        
    }
    
    //请求网络
    func requestNet() {
        
        //统一处理
        LDUserInfoNet.sharedInstance.queryGeTuiPushSetting({ [weak self] (dict) in
            
            guard let model = LDPushNoticeModel.yy_modelWithDictionary(dict as [NSObject : AnyObject]) else {
                return
            }
            
            self?.model = model
            self?.setDescTitleFromModel(model)
            
            }, failure: nil)
    }
    
    //
    func setNotiReq(params:NSDictionary) {
        
        LDUserInfoNet.sharedInstance.setGetuiPushSetting(params, success: {
            
            LDShowHUB.shareInstance.showBarStatus("成功")
        }, failure: { data in
                
            LDShowHUB.shareInstance.showBarStatus("失败")
                
        })
        
    }
    
    func setDescTitleFromModel(desc:LDPushNoticeModel) {
        
        if DDClientInfoManager.clientType() == DDClientType.Cattle {
            
            noticeValue.append(getTitle(desc.resume_interested))
            noticeValue.append(getTitle(desc.resume_viewed))
            noticeValue.append(getTitle(desc.newjob))
            
        } else {
            
            noticeValue.append(getTitle(desc.job_interested))
            noticeValue.append(getTitle(desc.job_viewed))
            noticeValue.append(getTitle(desc.newjobfinder))
            
            
        }
        
        NSOperationQueue.mainQueue().addOperationWithBlock { [weak self] in
            
            self?.tableView.reloadData()
        }
        
    }
    //set 提醒振动
    func setNoticeNotiType() {
        
//        GeTuiSdk.
        
    }
    
    //从model中读标题
    func getTitle(title:String?) ->String{
        
        if title == nil {
            
            return noticeTitleList[0]
        }
        switch title! {
        case "0":
            return noticeTitleList[0]
        case "1":
            return noticeTitleList[1]
//        case "2":
//            return noticeTitleList[2]
        default:
            return noticeTitleList[0]
        }
        
    }
    //从action中获取key
    func getKeyValue(index:Int) ->String {
        
        switch index {
        case 0:
            return selValueList[0]
            
        case 1:
            return selValueList[1]
            
//        case 2:
//            return selValue[2]
        default:
            return selValueList[0]
        }
    }
    
    //
    func setupTableView() {
        self.title = "提醒设置"
        tableView = LDBaseTableView()
        tableView.frame = view.frame
        tableView.delegate = self
        tableView.dataSource = self
        view.addSubview(tableView)
//        tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0)
//        tableView.separatorStyle = UITableViewCellSeparatorStyle.None
//        tableView.backgroundColor = LDBackGoundColorGray
    }
    
    // MARK: - Table view data source
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return dictTitleList.count
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return (dictTitleList[section]["title"]?.count)!
    }
    
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let sectionDic = dictTitleList[indexPath.section]
        let contentArr:Array<String> = sectionDic.objectForKey("title") as! Array
        
        
        if indexPath.section == 110 {
            
            var cell = tableView.dequeueReusableCellWithIdentifier("mySettingCellRemind_switch") as? mySettingCellRemind_switch
            if cell == nil {
                
                cell = mySettingCellRemind_switch.init(style: UITableViewCellStyle.Default, reuseIdentifier: "mySettingCellRemind_switch")
                cell?.titleLab!.text = contentArr[indexPath.row]
                cell?.titleLab?.font = LDFont.Font14
                
                cell?.tapSwitch({ [weak self] isSel in
                    
                    self?.setNoticeNotiType()
                })
            }
            
            return cell!
            
        } else {
            
            var cell = tableView.dequeueReusableCellWithIdentifier("niticeCellSetting") as? baseMySettingCells
            if cell == nil {
                
                cell = baseMySettingCells.init(style: UITableViewCellStyle.Default, reuseIdentifier: "niticeCellSetting")
                cell?.titleLab?.font = LDFont.Font14
                cell?.titleLab!.text = contentArr[indexPath.row]
                
            }
            
            /**每次要本地化读取*/
            if noticeValue.count == 3 {
                
                cell?.descLab?.text = noticeValue[indexPath.row]
            } else {
                
                cell?.descLab?.text = defaultTitleList[indexPath.row]
            }
            return cell!
        }
    }
    //didSelect
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
//        if indexPath.section == 1 {
        if indexPath.section == 0 {
            selectView = popActionSheetView()
            
            
            selectView!.selectSheetTextArr = noticeTitleList
            selectView!.showview((navigationController?.view)!)
            let secCell = tableView.cellForRowAtIndexPath(indexPath) as! baseMySettingCells
            
//            选择index
            selectView?.selectOfRows({ [weak self] (title,index) in
                
                switch indexPath.row {
                    
                    
                case 0:
                    secCell.descLab?.text = title
                    self?.selNotiIndex(indexPath, title: title, btnIndex: index)
                    
                case 1:
                    secCell.descLab?.text = title
                    self?.selNotiIndex(indexPath, title: title, btnIndex: index)
                    
                case 2:
                    secCell.descLab?.text = title
                    self?.selNotiIndex(indexPath, title: title, btnIndex: index)
//                case remindMeType.Now.rawValue:
//                    
//                    secCell.descLab?.text = title
//                    self?.selNotiIndex(indexPath, title: title, btnIndex: index)
//                    
//                case remindMeType.Day.rawValue:
//                    
//                    secCell.descLab?.text = title
//                    self?.selNotiIndex(indexPath, title: title, btnIndex: index)
//                    
//                case remindMeType.WeekDay.rawValue:
//                    
//                    secCell.descLab?.text = title
//                    self?.selNotiIndex(indexPath, title: title, btnIndex: index)
                    
                    
                default:
                    break
                }
                })
        }
    }
    
    func selNotiIndex(indexPath:NSIndexPath,title:String,btnIndex:Int) {
        
        let btnIndex = btnIndex - 1
        var params = NSDictionary()
        
        let selvalue = getKeyValue(btnIndex)
        
//        let type = jobfinderSelType[indexPath.row]
        
        if DDClientInfoManager.clientType() == DDClientType.Cattle {
            
            let type = jobfinderSelType[indexPath.row]
            params = ["type":type,"value":selvalue]
            setNotiReq(params)
            
        } else {
            
            let type = bossSelType[indexPath.row]
            params = ["type":type,"value":selvalue]
            setNotiReq(params)
        }
        
        
        
//        if DDClientInfoManager.clientType() == DDClientType.Cattle {
//            
//            if indexPath.row == 0 {
//                
//                params = NSDictionary()
//                
//                let selvalue = getKeyValue(btnIndex)
//                let type = jobfinderSelType[indexPath.row]
//                params = ["type":type,"value":selvalue]
//                
//                
//            } else if indexPath.row  == 1 {
//                
//                params = NSDictionary()
//                let selvalue = getKeyValue(btnIndex)
//                let type = jobfinderSelType[indexPath.row]
//                params = ["type":type,"value":selvalue]
//                
//            } else if indexPath.row == 2 {
//                
//                params = NSDictionary()
//                let selvalue = getKeyValue(btnIndex)
//                let type = jobfinderSelType[indexPath.row]
//                params = ["type":type,"value":selvalue]
//                
//            }
//            
//        } else {
//            
//            if indexPath.row == 0 {
//                
//                params = NSDictionary()
//                let selvalue = getKeyValue(btnIndex)
//                let type =  bossSelType[0]
//                params = ["type":type,"value":selvalue]
//                
//                
//            } else if indexPath.row  == 1 {
//                
//                params = NSDictionary()
//                let selvalue = getKeyValue(btnIndex)
//                let type = bossSelType[1]
//                params = ["type":type,"value":selvalue]
//                
//            } else if indexPath.row == 2 {
//                
//                params = NSDictionary()
//                let selvalue = getKeyValue(btnIndex)
//                let type = bossSelType[2]
//                params = ["type":type,"value":selvalue]
//                
//            }
//            
//        }
//        
//        setNotiReq(params)
        
    }
    
    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        
        return dictTitleList[section]["headtitle"] as? String
    }
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 60
    }
    
    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        let view = UIView()
        let text = dictTitleList[section]["headtitle"] as? String
        
        let lab = UILabel.setDetailsLab(text, fontSize: 14, titleColor: LDFontColor.DeepGray, Alignment: .Left)
        lab.frame = CGRectMake(LDMargin_V, 10, 200, 20)
        view.addSubview(lab)
        view.backgroundColor = LDBackGoundColorGray
        return view
    }
    
    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        return 40
    }
}
